package com.example.a_java.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/**
 * 数据库脚本执行工具
 * 用于在应用启动时插入部门数据
 */
@Component
public class DBScriptRunner implements CommandLineRunner {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("开始插入部门数据...");
        
        // 先清空现有数据
        jdbcTemplate.update("DELETE FROM sys_dept");
        
        // 插入部门数据
        jdbcTemplate.update("INSERT INTO sys_dept (parent_id, dept_name, leader, phone, email, status, sort_order, create_time, remark) VALUES (0, '总公司', '张三', '13900000001', 'zhangsan@example.com', 1, 1, NOW(), '公司总部')");
        jdbcTemplate.update("INSERT INTO sys_dept (parent_id, dept_name, leader, phone, email, status, sort_order, create_time, remark) VALUES (1, '研发部', '李四', '13900000002', 'lisi@example.com', 1, 2, NOW(), '负责产品研发')");
        jdbcTemplate.update("INSERT INTO sys_dept (parent_id, dept_name, leader, phone, email, status, sort_order, create_time, remark) VALUES (1, '市场部', '王五', '13900000003', 'wangwu@example.com', 1, 3, NOW(), '负责市场营销')");
        jdbcTemplate.update("INSERT INTO sys_dept (parent_id, dept_name, leader, phone, email, status, sort_order, create_time, remark) VALUES (1, '财务部', '赵六', '13900000004', 'zhaoliu@example.com', 1, 4, NOW(), '负责财务管理')");
        jdbcTemplate.update("INSERT INTO sys_dept (parent_id, dept_name, leader, phone, email, status, sort_order, create_time, remark) VALUES (1, '人事部', '孙七', '13900000005', 'sunqi@example.com', 1, 5, NOW(), '负责人事管理')");
        jdbcTemplate.update("INSERT INTO sys_dept (parent_id, dept_name, leader, phone, email, status, sort_order, create_time, remark) VALUES (2, '前端组', '周八', '13900000006', 'zhouba@example.com', 1, 6, NOW(), '负责前端开发')");
        jdbcTemplate.update("INSERT INTO sys_dept (parent_id, dept_name, leader, phone, email, status, sort_order, create_time, remark) VALUES (2, '后端组', '吴九', '13900000007', 'wujiu@example.com', 1, 7, NOW(), '负责后端开发')");
        jdbcTemplate.update("INSERT INTO sys_dept (parent_id, dept_name, leader, phone, email, status, sort_order, create_time, remark) VALUES (2, '测试组', '郑十', '13900000008', 'zhengshi@example.com', 1, 8, NOW(), '负责测试工作')");
        jdbcTemplate.update("INSERT INTO sys_dept (parent_id, dept_name, leader, phone, email, status, sort_order, create_time, remark) VALUES (3, '国内市场组', '冯一', '13900000009', 'fengyi@example.com', 1, 9, NOW(), '负责国内市场')");
        jdbcTemplate.update("INSERT INTO sys_dept (parent_id, dept_name, leader, phone, email, status, sort_order, create_time, remark) VALUES (3, '国际市场组', '陈二', '13900000010', 'chener@example.com', 1, 10, NOW(), '负责国际市场')");
        
        System.out.println("部门数据插入完成！");
    }
} 